<?php
/**
 * @author Oliver Leisalu
 * @package WapBilling
 */
class WapBilling_Emt extends WapBillingOperator {

	protected $terminalId;

	protected $merchantId;

	protected $url;
	

	public function pay( $price, $description, $uid = 0, $logId ) {
				
		$request = "<?xml version=\"1.0\" ?>\n";
		$request .= "<!DOCTYPE mcp-mpi SYSTEM \"mcp-mpi.dtd\">\n";
		$request .= "<mcp-mpi version=\"1.3\">\n";
		$request .= "  <FinancialTransactionRequest>\n";
		$request .= "    <PrimaryAccountNumber>".$this->getMsisdn()."</PrimaryAccountNumber>\n";
		$request .= "    <ProcessingCode>0</ProcessingCode>\n";
		$request .= "    <Amount>".round($price*100)."</Amount>\n";
		$request .= "    <STAN>".$uid."</STAN>\n";
		$request .= "    <DateTime>".date('Y-m-d\TH:i:sP')."</DateTime>\n";
		$request .= "    <CountryCode>978</CountryCode>\n";
		$request .= "    <FunctionCode>200</FunctionCode>\n";
		$request .= "    <TerminalID>".$this->terminalId."</TerminalID>\n";
		$request .= "    <MerchantID>".$this->merchantId."</MerchantID>\n";
		$request .= "    <ProductDescription>".$description."</ProductDescription>\n";
		$request .= "    <CurrencyCode>978</CurrencyCode>\n";
		$request .= "  </FinancialTransactionRequest>\n";
		$request .= "</mcp-mpi>\n";

		$log = new DomDBLogger('billing_emt');
		$logId = $log->log('pay-request', $request );
		
		// ---------------------------------
		// SEND REQUEST
		// ---------------------------------
		
		$ch = curl_init( $this->url );
		
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); 
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 3);
		
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		
		curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
		
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $request );
				
		$result = curl_exec($ch);
		curl_close($ch);
		
		$logId = $log->log('pay-result-'.$logId, $result );
		
		if( $result === false ) {
			$this->lastError = 'Sending data failed!';
			return 3;
		}

	    // ---------------------------------
		// PARSE RESULT
	    // ---------------------------------
	    
		$p = xml_parser_create();
		xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
		xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
		xml_parse_into_struct($p, $result, $vals, $index);
		xml_parser_free($p);

		$result = array();
		foreach( $vals as $val ) {
			if( $val['level'] == 3 )
				$result[$val['tag']] = $val['value'];
		}
		
		if( $result['ActionCode'] <= 99 ) {
			return 0;	
		} else {
			$this->lastError = 'Payment failed with error code '.$result['ActionCode'];
			return 1;
		}
	}

	public function getMsisdn() {
		$bar = trim( $_COOKIE['User-Identity-Forward-msisdn'] );
		$myout = '';
		for ($i=0; $i<strlen($bar)/2; $i++) {
			$myout.=chr(base_convert(substr($bar,$i*2,2),16,10));
		}
		return $myout;
	}

}
?>